/**
 * Created by Administrator on 2017/3/13.
 */
var page=1;
var local={};
$(function(){


    if(getQueryString('localLat')){
        local={"latitude":getQueryString('localLat'),"longitude":getQueryString('localLon')}
    }else{
        local=JSON.parse(sessionStorage.getItem('location'));
    }
   if(getQueryString('from')=='user'){
       ajaxmyPersonnel();
   }else{
       ajaxallPersonnel(1,6);
   }
});

//滚动加载
$(document.body).infinite();//初始化
var loading = false;  //状态标记
$(document.body).infinite().on("infinite", function() {
    if(loading) return;
    page++;
    loading = true;
    //$('.loadMore').css('display','block');
    $('.loadready').html('正在玩命加载中...');
    setTimeout(function() {
        if(getQueryString('id')){
            ajaxPersonnel(page,6,'append');
            loading = false;
        }else if(getQueryString('from')=='user'){
            ajaxmyPersonnel('append');
            loading = false;
        }else{
            ajaxallPersonnel(page,6,'append');
            loading = false;
        }

        //$('.loadMore').css('display','none');
        $('.loadready').html('');
    }, 700);   //模拟延迟
});


function ajaxallPersonnel(page,pagesize,append){
    //var local=JSON.parse(sessionStorage.getItem('location'));
    $.get(
        saleralllist,
        {
            page:page,
            pagesize:pagesize,
            lat:local.latitude,
            lng:local.longitude
        },
        function(res){
            var data = JSON.parse(res);
            console.log(data);
            if(data.length<1) {
                if(page==1) {
                    $.alert("暂无技师", "提示", function() {
                        location.href=indexUrl;
                    });
                    $(document.body).destroyInfinite();//停止加载
                    return false;
                }else{
                    $('.loadready').html('数据加载完毕').css('color','#aaa');
                    return false;
                }
            }
            var personnelHtml="";
            for (var i=0;i<data.length;i++ ) {
                var juli='';
                if(Number(data[i].distance)>2000){
                    juli=(Number(data[i].distance)/1000).toFixed(2)+'公里';
                }else{
                    juli=data[i].distance+'米';
                }
                var strRex=JSON.stringify(data[i]).replace(/"/ig,"'");//正在匹配，防止双引号报错
                personnelHtml+='<li onclick="gotoPerDetail('+ data[i].id+')"><i onclick="choosePersonnel(this)" data-id='+data[i].id+' data-name='+data[i].nickname+'></i>' +
                '<div><div class="lists_img"><img src='+data[i].avatar+' alt=""/></div>' +
                '<div class="lists_detail"><p class="detail_name"><span><b>'+data[i].nickname+'</b></span><span>' +
                '<i class="iconal-location"></i>'+juli+'</p>' +
                '<p class="detail_address"><span><span style="margin-right: 10px">'+data[i].sex+'</span>' +
                '<i class="iconal-wodedingdan"></i><span>'+data[i].scoreC+'</span>单</span><span>门店: <span>'+data[i].store.storename+'</span></span></p>' +
                '<p class="detail_details">'+data[i].content+'</p></div></div></li>';
            }
            page++;
            if(append=='append'){
                $('.lists ul').append(personnelHtml);
            }else {
                $('.lists ul').html(personnelHtml);
            }
        }
    );
}
function ajaxmyPersonnel(append){
    //var local=JSON.parse(sessionStorage.getItem('location'));
    $.get(
        myjishi,
        {
            lat:local.latitude,
            lng:local.longitude
        },
        function(res){
            var data = JSON.parse(res);
            console.log(data);
            if(data.length<1) {
                if(page==1) {
                    $.alert("暂无技师", "提示", function() {
                        location.href=indexUrl;
                    });
                    $(document.body).destroyInfinite();//停止加载
                    return false;
                }else{
                    $('.loadready').html('数据加载完毕').css('color','#aaa');
                    return false;
                }
            }
            var personnelHtml="";
            for (var i=0;i<data.length;i++ ) {
                var juli='';
                if(Number(data[i].distance)>2000){
                    juli=(Number(data[i].distance)/1000).toFixed(2)+'公里';
                }else{
                    juli=data[i].distance+'米';
                }
                var strRex=JSON.stringify(data[i]).replace(/"/ig,"'");//正在匹配，防止双引号报错
                personnelHtml+='<li onclick="gotoPerDetail('+ data[i].id+')"><i onclick="choosePersonnel(this)" data-id='+data[i].id+' data-name='+data[i].nickname+'></i>' +
                '<div><div class="lists_img"><img src='+data[i].avatar+' alt=""/>' +
                '</div>' +
                '<div class="lists_detail"><p class="detail_name"><span><b>'+data[i].nickname+'</b><span>'+data[i].sex+'</span></span><span>' +
                '<i class="iconal-location"></i>'+juli+'</p>' +
                '<p class="detail_address"><span><i class="iconal-wodedingdan"></i><span>'+data[i].scoreC+'</span>单</span><span>门店: <span>'+data[i].store.storename+'</span></span></p>' +
                '<p class="detail_details">'+data[i].content+'</p></div></div></li>';
            }
            page++;
            if(append=='append'){
                $('.lists ul').html(personnelHtml);
            }else {
                $('.lists ul').append(personnelHtml);
            }
        }
    );
}


function gotoPerDetail(id){
    location.href=personnelDetailUrl+'id='+id;
}



















